import router from './router'
// import { routelist } from '@/router/index'
import { Message } from 'element-ui'

import { getSessionToken, getToken, removeToken } from '@/utils/auth'

const whiteList = [
  '/login',
  '/homeScreen',
  '/secondaryScreen1',
  '/secondaryScreen2'
]

router.beforeEach((to, from, next) => {
  if (getSessionToken() || getToken()) {
    /* has token */
    if (to.path === '/login') {
      sessionStorage.removeItem('screenToken')
      removeToken()
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      Message.error('你还没有登录，请登录！！！')
      next('/homeScreen') // 否则全部重定向到登录页
    }
  }
})
